<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );
class EcommerceModelProducts extends JModel
{
	var $_data = null;
	var $_table = '#__pr_product';
	var $_list = null; 
	var $_page = null;
	var $_search = null;

	function &getTable()
	{
		if ($this->_table == null) {
			$this->_table = JTable::getInstance('products', $this->getDBO() );
		}
		return $this->_table;
	}

	function getList()
	{
		global $mainframe;

		if (!empty($this->_list)) {
			return $this->_list;
		}

		// Initialize variables
		$db		=& $this->getDBO();
		
		$search = null;
		$where = array();
		$layout_action = JRequest::getVar('layout','');
		if($layout_action=='')$layout_action = "products";
		
		$this->_search['model']			= $mainframe->getUserStateFromRequest("$layout_action.model", 'model', '',	'string');
		$this->_search['model']			= JString::strtolower($this->_search['model']);
		$this->_search['type']	= $mainframe->getUserStateFromRequest("$layout_action.type", 'type', 0,	'int');
		$limitstart			=  $mainframe->getUserStateFromRequest("$layout_action.limitstart", 'limitstart',0,	'int');
		
		
		$type = $this->_search['type'] ;
		if($type != 0 ){
			$where[] = ' a.type = '.$type;
		}
				
		if ($this->_search['model']) {
			$where[] = 'LOWER( a.model ) LIKE '.$db->Quote( '%'.$db->getEscaped( $this->_search['model']	, true ).'%', false ) ;
		}
		
		$where[] = ' a.user_id = 62';
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');

		// Get some variables from the request
		$option				= JRequest::getCmd( 'option' );
		$limit				= $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		
		// Get the total number of records
		$query = 'SELECT COUNT(*) FROM #__pr_product a ' . $where; //print $query ;
		$db->setQuery($query);
		$total = $db->loadResult();
		
		// Create the pagination object
		jimport('joomla.html.pagination');
		$this->_page = new JPagination($total, $limitstart, $limit);

		// Get the articles
		$query = 'SELECT a.* ' .
				' FROM #__pr_product as a ' .
				$where .
				' GROUP BY a.id ORDER BY a.ordering';
		//print_r ($query);
		$db->setQuery($query, $this->_page->limitstart, $this->_page->limit);
		$this->_list = $db->loadObjectList();

		//If there is a db query error, throw a HTTP 500 and exit
		if ($db->getErrorNum()) {
			JError::raiseError( 500, $db->stderr() );
			return false;
		}

		return $this->_list;
	}
	
	function getPagination()
	{
		if (is_null($this->_list) || is_null($this->_page)) {
			$this->getList();
		}
		return $this->_page;
	}
	
	function getSearch()
	{
		return $this->_search;
	}

}

?>
